package com.y.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.y.config.exception.BusinessException;
import com.y.entity.Food;
import com.y.entity.Order;
import com.y.entity.vo.R;
import com.y.entity.vo.RHttpStatusEnum;
import com.y.mapper.OrderMapper;
import com.y.service.FoodService;
import com.y.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.y.utils.Assert;
import com.y.utils.ConstantName;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yjh
 * @since 2021-09-24
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    OrderMapper orderMapper;
    @Resource
    FoodService foodService;


    /**
     * 获取用户购物车中商品
     * @param orderStatus 根据订单状态
     * @return
     */
    @Override
    public List<Order> showUserOrderByStatus(Integer orderStatus) {
        List<Order> orders = orderMapper.showOrderByUserID(StpUtil.getLoginIdAsInt());

        List<Order> list = orders.stream()
                .filter((order) -> order.getStatus().equals(orderStatus))
                .collect(Collectors.toList());
        list.forEach(System.out::println);
        return list;
    }

    @Override
    public List<Order> showAllOrderByStatus(Integer status) {
        List<Order> orders = orderMapper.showAllOrderByStatus();

        return orders.stream()
                     .filter((order) -> order.getStatus().equals(status))
                     .collect(Collectors.toList());
    }

    @Override
    public List<Order> showAllOrderByStatus() {
        return orderMapper.showAllOrderByStatus();
    }

    /**
     * 获取订单
     * @param oId
     * @return
     */
    @Override
    public Order getOneOrderByOID(Integer oId) {
        List<Order> orders = orderMapper.showOrderByUserID(StpUtil.getLoginIdAsInt());
        List<Order> list = orders.stream()
                .filter((order) -> order.getId().equals(oId))
                .collect(Collectors.toList());
        return list.get(0);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public R addOrder(Integer fId, Integer nums) {
        Order order = new Order();
        // 存入订单 当前用户的id 食品id 数量
        order.setUId(StpUtil.getLoginIdAsInt())
                .setFId(fId)
                .setNums(nums);
        System.out.println("添加订单order = " + order);
        return  this.save(order)?R.success("加入成功"):R.error("添加失败");
    }

    /**
     * 购买商品
     * @param oId 订单ID
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R buyOrder(Integer oId) {
        // 更新订单状态
        Order order = orderMapper.selectById(oId);
        order.setStatus(ConstantName.ORDER_BUYING_STATUS);

        // 更新商品库存
        Food food = foodService.getById(order.getFId());
        Assert.isTrue( food.getNums() >= order.getNums(),new BusinessException(RHttpStatusEnum.FOOD_NOT_ENOUGH));
        food.setNums(food.getNums() - order.getNums());
        boolean success =  this.updateById(order) && foodService.updateById(food);
//        int i = 10 /0;
        return (success) ? R.success("购买成功") : R.error("购买失败,请稍后再试");
    }

    /**
     * 移除商品
     * @param oId 订单id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public R removeOrder(Integer oId) {
        Order order = orderMapper.selectById(oId);
        order.setStatus(ConstantName.ORDER_REMOVE_STATUS);

        return this.updateById(order) ? R.success("删除成功") : R.error("删除失败");
    }

    /**
     * 修改订单信息
     * @param oId 订单id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Order editOrder(Integer oId) {
        List<Order> orders = orderMapper.showOrderByUserID(StpUtil.getLoginIdAsInt());
        List<Order> list = orders.stream()
                .filter((order) -> order.getId().equals(oId))
                .collect(Collectors.toList());

        return list.get(0);
    }


    /**
     *  更新修改订单信息
     * @param oId 订单id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public R editOrderUpdate(Integer oId, Integer nums) {
        Order order = orderMapper.selectById(oId);
        order.setNums(nums);
        return saveOrUpdate(order)?R.success("修改成功"):R.error("修改失败");
    }

}
